package com.cb.kfc.controller;

import com.cb.kfc.domain.Member;
import com.cb.kfc.service.IMemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    //查询全部个人信息
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required = true,defaultValue = "4")Integer size) throws Exception{
        ModelAndView mv = new ModelAndView();
        List<Member> memberList = memberService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(memberList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("member-page-list");
        return mv;
    }

    //个人信息添加
    @RequestMapping("/save.do")
    public String save(Member member) throws Exception {
        memberService.save(member);
        return "redirect:findAll.do";
    }

    /**
     * 删除订单
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = memberService.deleteProductById(ids);
        return "redirect:findAll.do";
    }

    /**
     *展示详情的功能
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name="id",required = true) String id) throws Exception{
        ModelAndView mv = new ModelAndView();
        Member member = memberService.findById(id);
        mv.addObject("member",member);
        mv.setViewName("member-show");
        return mv;
    }
    /**
     *特权转赠
     * @return
     * @throws Exception
     */
    @RequestMapping("/give.do")
    public String  give(@RequestParam(name="orderid",required = true) String orderid,@RequestParam(name="friendid",required = true) String friendid) throws Exception{
        memberService.give(orderid,friendid);
        return "redirect:/orders/findAll.do";
    }

    /**
     * 删除
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteFriend.do")
    public String deledeleteFriendte(@RequestParam(name="ids",required = true) String[] ids) throws Exception{
        Integer  integer = memberService.deleteFriend(ids);
        return "redirect:/orders/findAll.do";
    }

    @RequestMapping("/member-add.do")
    public String  memberAdd()  {
         return "member-add";
    }
    @RequestMapping("/member-give.do")
    public String  memberGive()  {
        return "member-give";
    }

//    更新会员个人信息
    @RequestMapping("/update.do")
    public String Update(Member member) throws Exception {
        memberService.update(member);
        return "redirect:findAll.do";

    }
}
